﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Windows.Forms;
using DataLayer;

namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ShadowWindow : Window
    {
        static public DataLayer.ImageContent returnImageContent=null;
        static ShadowWindow instance = null;
        static public BitmapSource returnImageSource;

        public static ShadowWindow Instance
        {
            get { return ShadowWindow.instance; }
            set { ShadowWindow.instance = value; }
        }
        public double x;
        public double y;
        public double width;
        public double height;
        public bool isMouseDown = false;
        public string path;
        public ShadowWindow(string dir)
        {
            InitializeComponent();
            path = dir;
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
            x = e.GetPosition(null).X;
            y = e.GetPosition(null).Y;
        }

        private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            try
            {
                if (this.isMouseDown)
                {
                    double curx = e.GetPosition(null).X;
                    double cury = e.GetPosition(null).Y;

                    System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
                    SolidColorBrush brush = new SolidColorBrush(Colors.White);
                    r.Stroke = brush;
                    r.Fill = brush;
                    r.StrokeThickness = 0;
                    r.Width = Math.Abs(curx - x);
                    r.Height = Math.Abs(cury - y);
                    cnv.Children.Clear();
                    cnv.Children.Add(r);
                    Canvas.SetLeft(r, Math.Min(x, curx));
                    Canvas.SetTop(r, Math.Min(y, cury));
                    if (e.LeftButton == MouseButtonState.Released)
                    {
                        cnv.Children.Clear();
                        width = Math.Abs(e.GetPosition(null).X - x);
                        height = Math.Abs(e.GetPosition(null).Y - y);
                        returnImageSource = this.CaptureScreen(x, y, width, height);
                        this.x = this.y = 0;
                        this.isMouseDown = false;
                        this.Close();
                        DateTime now = DateTime.Now;
                        string newPath = this.path+"/"+"abc" + now.Second.ToString() + now.Minute.ToString() + now.Hour.ToString() + now.Day.ToString() + now.Month.ToString() + now.Year.ToString() + ".png";
                        currentBitmap.Save(newPath, ImageFormat.Png);
                        //int returnID=ResourcesHelper.GetInstance().AddResource(newPath);
                        returnImageContent = new ImageContent(newPath);
                        returnImageContent.Type = "image";
                    }
                }
            }
            catch (Exception eX)
            { 
                
            }
        }
        Bitmap currentBitmap;
        public BitmapSource CaptureScreen(double x, double y, double width, double height)
        {
            int ix, iy, iw, ih;
            ix = Convert.ToInt32(x);
            iy = Convert.ToInt32(y);
            iw = Convert.ToInt32(width);
            ih = Convert.ToInt32(height);
            currentBitmap = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);        
            Graphics g = Graphics.FromImage(currentBitmap);
            g.CopyFromScreen(ix-7, iy-7, 0, 0,
            new System.Drawing.Size(iw, ih),
            CopyPixelOperation.SourceCopy);
           
          
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(currentBitmap.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(iw, ih));
            ;
        }

        public void SaveScreen(double x, double y, double width, double height)
        {
            const int SRCCOPY = 0xcc0020;
            int ix, iy, iw, ih;
            ix = Convert.ToInt32(x);
            iy = Convert.ToInt32(y);
            iw = Convert.ToInt32(width);
            ih = Convert.ToInt32(height);
            try
            {
                Bitmap myImage = new Bitmap(iw, ih);

                Graphics gr1 = Graphics.FromImage(myImage);
                IntPtr dc1 = gr1.GetHdc();
                IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetForegroundWindow());
                NativeMethods.BitBlt(dc1, ix - 7, iy - 7, iw, ih, dc2, ix-7, iy-7, SRCCOPY);// 13369376);
                gr1.ReleaseHdc(dc1);
                myImage.Save(DateTime.Now.ToString(), ImageFormat.Png);
            }
            catch { }
        }
        public static void CaptureScreen(string path)
        {
            if (ShadowWindow.Instance == null)
            {
                ShadowWindow win = new ShadowWindow(path);
                win.WindowState = WindowState.Maximized;
                win.ShowDialog();
            }
        }
        internal class NativeMethods
        {
            [DllImport("user32.dll")]
            public extern static IntPtr GetDesktopWindow();
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowDC(IntPtr hwnd);
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetForegroundWindow();
            [DllImport("gdi32.dll")]
            public static extern UInt64 BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, System.Int32 dwRop);

        }

        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                this.Close();
            }
        }

    }
}
